ladder = {}
-- INPUT
function ladder:Input(playAgain) end
-- OUTPUT
function ladder:Show() end -- show dialog
function ladder:Hide() end -- hide dialog

----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------

ladder.layer = nil

function ladder:Input(playAgain)
    self.playAgain = playAgain
end
function ladder:PlayAgain()
    self:Hide()
    self.playAgain()
end

function ladder:ViewLadder()
    self:Hide()
    Ladder:Show()
end

function ladder:SetData(parent, rank, name, score)
    local pos = CCPointMake(0, pplCfg.RESOLUTION.height - 205 - 100 * rank)

    local l;
    l = CCLabelTTF:create(""..rank, "Arial", 30)
    l:setAnchorPoint(CCPointMake(0, 1))
    l:setPosition(CCPointMake(pos.x + 120, pos.y))
    parent:addChild(l)
    
    l = CCLabelTTF:create(name, "Arial", 30)
    l:setAnchorPoint(CCPointMake(0, 1))
    l:setPosition(CCPointMake(pos.x + 180, pos.y))
    parent:addChild(l)
    
    l = CCLabelTTF:create(""..score, "Arial", 30)
    l:setAnchorPoint(CCPointMake(1, 1))
    l:setPosition(CCPointMake(pplCfg.RESOLUTION.width - 120, pos.y))
    parent:addChild(l)
end

function ladder:Create()
    self.layer = CCLayer:create();
    self.layer:setPosition(CCPointMake(0, 100))
    
    local bg = CCSprite:create("png/ladderbg.png");
    bg:setPosition(pplCfg.RESOLUTION.width/2, pplCfg.RESOLUTION.height/2);
    self.layer:addChild(bg);

    for i=1, 5 do
        local sprite = CCSprite:create("png/laddersub.png")
        sprite:setPosition(CCPointMake(pplCfg.RESOLUTION.width/2, pplCfg.RESOLUTION.height - 222 - 100 * i))
        self.layer:addChild(sprite)
    end

    for i=1, 5 do
        self:SetData(self.layer,  i, "liubin"..i, 3000+1990*(5-i)*(5-i))
    end

    local function PlayAgain()
        self:PlayAgain();
    end

    --local function ViewLadder()
    --    self:ViewLadder()
    --end

    local item1 = CCMenuItemImage:create("png/playAgain1.png", "png/playAgain2.png")
    item1:registerScriptTapHandler(PlayAgain)
    item1:setPosition(0, -400);

    local itemC = CCMenuItemImage:create("png/close1.png", "png/close2.png")
    itemC:registerScriptTapHandler(PlayAgain)
    itemC:setPosition(253, 277);

    --local item2 = CCMenuItemImage:create("viewLadder1.png", "viewLadder2.png")
    --item2:registerScriptTapHandler(ViewLadder)
    --item2:setPosition(0, -100);

    local menu = CCMenu:create()
    menu:addChild(item1)
    menu:addChild(itemC)
    --menu:addChild(item2)
    self.layer:addChild(menu)

    self.parent:addChild(self.layer, 1)

    -- animation
    self.layer:setScale(0)
    local ac = CCScaleTo:create(0.4, 1.0, 1.0)
    self.layer:runAction(ac)
end

function ladder:Show()
    if (self.layer == nil) then
        self.parent = GetMainScene()
        self:Create()
    end
    self.layer:setVisible(true)
end

function ladder:Hide()
    if self.layer then
        self.layer:setVisible(false)
    end
end